﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using VmGui.localhost;
using System.Web.Services.Protocols;


namespace VmGui
{
    public partial class FormNovoRepresentada : Form
    {
        FormCrudRepresentada fcr = new FormCrudRepresentada();

        public FormNovoRepresentada()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Service1 sv = new Service1();
                Representada repre = new Representada();

                repre.RazaoSocial = textBox1.Text;
                repre.Cnpj = maskedTextBox2.Text.Replace("-", "").Replace("_", "").Replace("/", "").Replace(".", "").Replace(" ", "");
                repre.Endereco = textBox3.Text;
                repre.Cidade = textBox4.Text;
                repre.Uf = comboBox1.SelectedItem.ToString();
                repre.Contato = textBox6.Text;
                repre.Telefone = maskedTextBox1.Text.Replace("(", "").Replace(")", "").Replace("-", "").Replace("_", "").Replace(" ", "");

                if (sv.Consultar_CnpjRepre(repre) == false)
                {
                    sv.Incluir_repre(repre);
                    MessageBox.Show("Registro cadastrado com Sucesso", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    fcr.listar_Representada();
                }
                else
                {
                    MessageBox.Show("CNPJ já cadastrado!", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                if (MessageBox.Show("Deseja incluir um novo registro?", "Atenção", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    textBox1.Clear();
                    maskedTextBox2.Clear();
                    textBox3.Clear(); ;
                    textBox4.Clear(); ;
                    textBox6.Clear(); ;
                    maskedTextBox1.Clear();

                    textBox1.Focus();
                    fcr.listar_Representada();
                }
                else
                {

                    Close();
                }
            }
            catch (SoapException erro)
            {
                var message = erro.Message;
                message = message.Remove(0, message.IndexOf(":") + 1);

                if (message.IndexOf('\n') >= 0)
                {
                    message = message.Remove(message.IndexOf('\n'));
                }
                MessageBox.Show(message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            catch (NullReferenceException)
            {
                MessageBox.Show("Você deve selecionar um estado", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

        }
    }
}